<?php

namespace app\base\controller\api;

use app\base\model\RedianData;
use app\base\service\WsSendService;
use think\admin\Controller;
use app\base\model\AiDevice;
use app\base\controller\api\Auth;
use app\base\model\AiVoice;


/**
 * 设备相关
 * Class Login
 * @package app\store\controller
 */
class MobileScript extends Controller
{
    //更新数据状态
    public function updateData()
    {
        $data = $this->app->request->get();
        $schedule_id =   RedianData::mk()->where(['id'=>$data['id']])->value('schedule_id');
        RedianData::mk()->where(['id'=>$data['id']])->save($data);
        $this->app->db->name("ai_device")->where(['id'=>$schedule_id])->update(['work_status'=>1]);
        $this->success('更新成功');
    }

    public function getScriptNext()
    {
        $id = $this->app->request->get('id');
        $now_script_file = $this->app->request->get('now_script_file');
        ///storage/emulated/0/Download/lyzk/script/kaipa.js
        //判断$now_script_file中是否包含script,如果包含则取kaipa.js
        if(strpos($now_script_file, 'script') !== false){
            $now_script_file = basename($now_script_file);
            $this->app->log->error("提取脚本文件名: {$now_script_file}");
        }

        $data = RedianData::mQuery()->where(['id'=>$id])->find();
        $run_script = json_decode($data['run_script'],true);
        $next_script_file = "";
        $this->app->log->error("now_script_file:".$now_script_file);
        $this->app->log->error("getScriptNext:".json_encode($run_script));
        foreach ($run_script as $key=>$value){
            if($value==$now_script_file && isset($run_script[$key+1])){
                $next_script_file = $run_script[$key+1];
                break;
            }
        }
        $this->app->log->error("next_script_file:".$next_script_file);
        if($next_script_file){
            return 1;
//            $content = [
//                'type'=>1,
//                'script_files'=>[$next_script_file],
//                'script_file'=>$next_script_file,
//                'next_script_file'=>$next_script_file,
//                'data_id'=>$data['id'],
//                'is_update'=>1
//            ];
//            WsSendService::instance()->send($data['schedule_id'],1,$content);

        }else{
            $this->app->db->name("ai_device")->where(['id'=>$data['schedule_id']])->update(['work_status'=>1]);
             RedianData::mQuery()->where(['id'=>$id])->update(['is_flish'=>1]);
        }

    }

    //判断是否需要下载视频(废弃)
    public function isDownloadVideoB(){
        $data = $this->app->request->get();
        $task_id =   RedianData::mk()->where(['id'=>$data['id']])->value('task_id');
        $is_auto_jjjb = $this->app->db->name("redian_task")->where(['id'=>$task_id])->value("is_auto_jjjb");
        if($is_auto_jjjb==1){
            return 2;
            $this->success('不需要下载视频',1);
        }else{
            return 1;
            $this->success('需要下载视频',0);
        }
    }

    //判断是否需要下载视频
    public function isDownloadVideo(){
        $data = $this->app->request->get();
        $is_auto_jjjb =   RedianData::mk()->where(['id'=>$data['id']])->value('is_auto_jjjb');
        //$is_auto_jjjb = $this->app->db->name("redian_task")->where(['id'=>$task_id])->value("is_auto_jjjb");
        if($is_auto_jjjb==1){
            return 2;
            $this->success('不需要下载视频',1);
        }else{
            return 1;
            $this->success('需要下载视频',0);
        }
    }

    //拉取弹窗关闭文字
    public function getCloseText(){
        $type = $this->app->request->get('type');
        $text = $this->app->db->name("script_close_font")->where(['status'=>1])->whereIn('app_type',"0,$type")
            ->order('sort asc,id desc')->column('text');
        //return implode("|",$text);
        $this->success('成功',$text);
    }
}
